<?php
namespace Components\Account\Backstage;

class Validation extends \Phalcon\Validation {
	private $fields;

	public function execute($data) {
		$result = [];
		$validationMessages = $this -> validate($data);
		if($validationMessages -> valid()) {
			$result['code'] = -1;
			$result['message'] = $validationMessages[0]->getMessage();
		} else {
			$result['code'] = 0;
			$result['data'] = $data;
		}
		return $result;
	}

	public function addAccount() {
		return $this -> rules('account',[
			new \Phalcon\Validation\Validator\PresenceOf(),
			new \Phalcon\Validation\Validator\StringLength([
				'min' => 6
			])
		]);
	}

	public function addAccountUniqueness() {
		return $this -> rules('account',[
			new \Phalcon\Validation\Validator\PresenceOf(),
			new \Phalcon\Validation\Validator\StringLength([
				'min' => 6
			]),
			new \Phalcon\Validation\Validator\Uniqueness([
				'model' => new Model(),
				'message' => '账号已存在'
			])
		]);
	}

	public function addPassword() {
		return $this -> rules('password',[
			new \Phalcon\Validation\Validator\PresenceOf(),
			new \Phalcon\Validation\Validator\StringLength([
				'min' => 6
			])
		]);
	}

	public function addStatus() {
		return $this -> rules('status',[
			new \Phalcon\Validation\Validator\InclusionIn([
				'domain' => [0, 3]
			])
		]);
	}
}
